One thread controlling many others

I have an application that waits for clients to connect. Each time a client connects, a new frame gets created (with the new socket file descriptor). I know how many clients will connect, after I reach that number I just run pthread_join in a for loop.

My problem is that I would like the main thread to control all the other threads. My goal is to have each thread send the same message back to the client, at the same time, and only once. There are multiple messages a thread can send.

My current thinking is to define a list of command, as follows:

char *commands[] = {
                (char*) "TERMINATE\0",
                .... };

And then specify a command number that represents which command to use in that char* array. All threads will do something like

write(sockfd, buffer[commandNumber], length[commandNumber]);

I thought about waiting on a condition variable, but I see two problems:

1) I want to make sure that each thread, although synchronized, execute the command only once.

2) The main thread that initiates the command has to know when all those threads is done executing the command.

Only way I see to execute 2) is to keep track of a counter (with mutexes), and when each thread executes the command, it can increase that counter. I am not sure I will be able to avoid a thread from running the command twice.

What is the best possible way please to coordinate multiple threads to execute a single action at once; and also be able to know when that action has finished executing for every thread please?


You might use a barrier to gate the operation.

Synchronizing the send

The main thread initializes a barrier named "Ready" to N+1. Then it begins accept()ing N client connections, spawning a worker thread for each. The new worker threads immediately wait on barrier "Ready".

After spawning the Nth (and last) worker, the main thread sets the desired command (perhaps using a global commandNumber). Then the main thread waits on barrier "Ready". As soon as all workers and the main thread have arrived (reaching the barrier's limit of N+1), all threads are released, knowing that they are ready to issue their command immediately.

(A common alternate approach is to use a predicate and condition variable rather than a barrier. For example, the main thread might spawn the Nth worker and then cond_broadcast() that it has set a flag ready = 1. This approach is flawed. The main thread cannot know that the Nth worker — or, indeed, any of the workers — are yet waiting on that condition. The barrier solves this problem.)

Indicating completion

Another N+1 barrier, "AllDone", could be used to indicate that the workers are all done. A semaphore initialized to -N and posted by workers would do the same. Having the workers close() their connections and the main thread select()ing or poll()ing connections would convey the same information, too.

Need Your Help

Can I use OpenCL with Monogame?

opengl-es opencl opentk monogame cloo

I'm looking at Monogame for developing with. The site mentions it has OpenTK "under the hood". Does this mean I could have solid access to OpenCL using Monogame, or has OpenCL been split off into C...

jquery autocomplete is not working properly

javascript php jquery ajax autocomplete

I have written the code below. Simply I want to get source array with ajax from userfunctions.php.. It works but after few miliseconds the selection div disappears. Also when I don't use

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.