The way I am waiting on a thread to start is by creating an event, and when I create a thread, I pass it this event, and when the thread starts executing, it signals this event (which I am already waiting on). I have no problem with this approach, but I am wondering if Windows provides a special function for this.
Your current solution is a perfectly good solution to the problem. You've no reason or need to look for a better solution. Certainly the threading API doesn't offer you any built-in mechanism so you do have to implement something. And what you are doing is a fine way to solve the problem.
Related
Is anyone here familiar with this open-source particle emitter CBEffects? It's pretty great and generally straightforward but somehow whenever I call vent:stop(), which behind the scenes cancels the emitter timer, all timers get cancelled. Did anyone experience incidents similar to that? I checked the source code and it's definitely cancelling the right timer.
(Posted on behalf of the OP).
Never mind. I just found out, that it was due to an updated timer library.
I have an app that has multiple threads. Some of the threads are using libraries that are built on top of the pthread API.
Reading through the docs it seems that at one time you had to call gdk_threads_enter and gdk_threads_leave.
But looking at the docs, the whole lot seems to be deprecated. Now they are saying you need to do everything from the main thread. But after the main_thread enters gtk_main() how am I supposed to signal that thread to e.g. refresh some widgets like a menu I'm building dynamically from another thread which pulls the menu items from a REST web service and is long running. I assume I shouldn't just do that from the other thread according to the documentation. Although it's guarded with a lock and still appears to work. I do get occasional crashes and I'm not really sure if it's because of this this.
Gtk:: widgets are not thread safe. So, never update Gtk::Widget inside a thread, update the widget inside a timer function which gets started on a regular period.
I find it hard to believe there isn't an answer or tutorial for this, but am struggling to find one anywhere!
I have to (and have) build a multithreaded server to handle GET requests in C.
For full marks this needs to use a thread pool. Currently my main thread accepts connections and passes them on to a new thread.
I can find a few implementations of thread pools in c online, but coming from a Java background understanding them is proving difficult. They also all seem to use a task queue.
This seems unnecessary considering you can tell the listen call to queue connections.
I saw somewhere that accept is thread safe (saying that I also hear when POSIX says safe its more of a safeish?)
Is this a sensible approach to take? Or will the overhead be higher with each thread waiting on accept instead of stopping exection until passed a connection?
If that is the case how in C would I go about doing this? I presume i would need to keep a thread safe data structure storing pointers to each thread and a value indicating if they are busy or not?
And have some method to restart the thread and pass it a connection? But I have no idea how to do this and can't find any simple tutorials on the internet.
Any advice or links to tutorials would be much appreciated!
Thanks
Accept() is thread-safe.
Actually what you describe is an elegant way to implement a socket server using a thread pool - call accept() in all of them, and the operating system will take care of waking only one thread when a connection arrives. Good job, I have never really thought about this option when I had to implement such things.
As far as I see there's no real overhead in calling accept() in multiple threads at the same time - all threads will sleep until a connection can be accepted, so they won't effectively consume any CPU time.
Is it possible to make a synchronous network call on UI thread in WPF (Windows Phone 8).
(I know it's cons, but still i need this functionality to make it work with some ported code)
Tried using autoresetevent method. Due to deadlock, it is blocking ui thread and app hangs forever.
Even tried with webclient. still the UI thread is getting blocked and app hangs forever.
Any help...
This is a very bad idea - I've found that doing this by accident will actually lead to a complete deadlock of your UI. You need to use an asynchronous method.
Callbacks from asynchronous network operations are queued on the UI thread. If you block the thread to wait for it, the callback will never arrive because it is waiting for access to the same thread.
You do have another option though... If you have ported the code, then you will have to change it to support async operations.
I an new at working with IPC objects with synchronization purposes for processes and threads.
Is there any special way to implement a Monitor in C? (such as semaphores, pipes, sockets, etc.) Is there a special .h file that has a specific object to use here?
If not, which is the best way to implement this?
Thanks in advance!
Silvio.
I'd use select, it works pretty much for everything you need: http://linux.die.net/man/2/select
Beyond that, I usually use the pthread style functions for mutexes/semaphores, but it's really down to what the task at hand actually is.
Best way would be a separate process. When a process starts it registers this fact to the monitor process. Then the monitor process can use calls to check if the process that registered with it is still running
Depends on what your scenario but my guess is you want something that requires very little setup to implement is which case I'd go with septical.