2

In Win32, you can create a thread in suspended mode, by using the dwCreationFlags parameter with CREATE_SUSPENDED. I am looking for a similar functionality with pthreads. Note that I don't want to suspend the thread after running it and then pausing it by using condition variables, but actually create it in suspended mode and then start it later on.

The advantage of using this approach is that I can assign some properties to that thread before running it. For example, bind it to a certain core before starting, which is more efficient than first starting and then assigning it to a core, as it might get moved from one core to another.

If not possible, can we at least bind a thread to a core when calling pthread_create?

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356

3 Answers3

6

If you want to bind a thread to a CPU right from the start, you can use the form of pthread_create with a pthread_attr_t argument. Linux suports a special attribute pthread_attr_setaffinity_np, which allows the binding of a thread to a certain CPU set. Do not confuse this with pthread_setaffinity_np which requires an already running thread.

The plan of action is this:

// create generic attribute set
pthread_attr_t attr;
pthread_attr_init(&attr);

// enhance with CPU set
pthread_attr_setaffinity_np(&attr, ...cpuset-args);

// create thread with right attributes including CPU set
pthread_t thread;
pthread_create(&thread, &attr, ...);

// viola, thread runns on given CPU-set, cleanup
pthread_attr_destroy(&attr);
A.H.
  • 63,967
  • 15
  • 92
  • 126
0

Initial suspended state or core binding would be handled by attributes given in the second argument of pthread_create, with the options documented in pthread_attr_init's manpage. I haven't found relevant flags there, so it seems to be currently not possible. You could file a bug against the Linux pthreads implementation.

thiton
  • 35,651
  • 4
  • 70
  • 100
0

There are no flags currently on pthread attributes to create a thread in suspended state.

It is likely that you have to suspend it manually after creation.

For implementing suspending/resume, you can have a look here

Community
  • 1
  • 1
ziu
  • 2,634
  • 2
  • 24
  • 39
  • At least, can we bind it to a core at pthread_create time? By the way, I hate that condition variable method to suspend, so inefficient! – MetallicPriest Oct 31 '11 at 13:04
  • you're looking for this [function](http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_setaffinity_np.3.html) – ziu Oct 31 '11 at 14:22