473,624 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threads with STL - can I run faster?

Here is a small sample program that I have.

#include <stdlib.h>
#include <pthread.h>
#include <string>

using namespace std;

pthread_t threads[10];
pthread_attr_t thr_attr;
int thr_in[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int totalIter = 0;
int thr_cnt = 0;
bool debug = false;

extern "C" void *do_something(v oid *tid);

int main( int argc, const char* argv[] )
{
int thr_var = 0;

//------------------
// how many threads?
//------------------
thr_cnt = atoi(argv[1]);
if (thr_cnt > 8)
{
cout << "WARNING: Limiting the thread count to 8" <<
endl;
thr_cnt = 8;
}

//--------------------------
// how much work to be done?
//--------------------------
totalIter = atoi(argv[2]);
if (totalIter > 5000000)
{
cout << "WARNING: Limiting the iteration count to
5000000" << endl;
totalIter = 5000000;
}

//-------------------------------
// do you want to check up on me?
//-------------------------------
if (argv[3] != NULL) debug = true;

//--------
// threads
//--------
pthread_attr_in it(&thr_attr);
pthread_attr_se tdetachstate(&t hr_attr,
PTHREAD_CREATE_ JOINABLE);

for (thr_var = 1; thr_var<=thr_cn t; thr_var++)
pthread_create( &threads[thr_var], &thr_attr,
do_something, (void *)
&(thr_in[thr_var]));

for (thr_var=0; thr_var<thr_cnt ; thr_var++)
pthread_join(th reads[thr_var], NULL);

pthread_attr_de stroy(&thr_attr );

return 0;
}
void *do_something(v oid *tid)
{
int myThreadId = *((int *)tid);
FILE *fp = NULL;
if (debug)
{
char filename[50] = "";
sprintf(filenam e, "%d.out", myThreadId);
fp = fopen(filename, "w");
fprintf(fp, "thread #%d processing starts\n",
myThreadId);
}

for (int i=1; i<=totalIter; i++)
{
if (i%thr_cnt == myThreadId-1)
{
if (debug)
{
fprintf(fp, "thread #%d processing
index %d\n", myThreadId, i);
}

string a("abc"), b;
b = a;
}
}

if (debug)
{
fprintf(fp, "thread #%d processing finish\n",
myThreadId);
fflush(fp);
fclose(fp);
}

pthread_exit(NU LL);
return NULL;

}
Now when I run this with 1 thread, here is the time taken.

/home/skher/testIPC/testThr> time $BIN/testThr 1 5000000

real 0m0.65s
user 0m0.47s
sys 0m0.14s
/home/skher/testIPC/testThr>

impressive, considering I am doing 5 million iterations. So, I thought
when I run with 2 or more threads, I should be done even in less time.
But here is what I found.

/home/skher/testIPC/testThr> time $BIN/testThr 2 5000000

real 0m34.67s
user 0m58.48s
sys 0m5.20s
/home/skher/testIPC/testThr>

Why is this? I guess this is because whenever I allocate any STL
object, using the _node_alloc template defined in _alloc.c, it has a
lock and unlock mechanism using a static class _Node_Alloc_Loc k which
has a static member variable.

Part of that class code is shown here.

template <bool __threads, int __inst>
class _Node_Alloc_Loc k {
public:
_Node_Alloc_Loc k() {

# ifdef _STLP_SGI_THREA DS
if (__threads && __us_rsthread_m alloc)
# else /* !_STLP_SGI_THRE ADS */
if (__threads)
# endif
_S_lock._M_acqu ire_lock();
}

~_Node_Alloc_Lo ck() {
# ifdef _STLP_SGI_THREA DS
if (__threads && __us_rsthread_m alloc)
# else /* !_STLP_SGI_THRE ADS */
if (__threads)
# endif
_S_lock._M_rele ase_lock();
}

static _STLP_STATIC_MU TEX _S_lock;
};
OK. Now my (worth million dollar only to me) question.

How do I get around this? How do I make my program run faster with more
threads. If you see, the threads are really mutually exclusive since
they are working on different indexes (indices) but still compete with
each other for resources viz. lock while creating STL object. How do I
make this competition go away thus making my program run faster with
more threads.

Any help will be appreciated. Thanx, Sunil.

Nov 23 '05 #1
4 1957
On 2005-11-23, su*******@hotma il.com <su*******@hotm ail.com> wrote:
OK. Now my (worth million dollar only to me) question.

How do I get around this? How do I make my program run faster
with more threads. If you see, the threads are really mutually
exclusive since they are working on different indexes (indices)
but still compete with each other for resources viz. lock while
creating STL object. How do I make this competition go away
thus making my program run faster with more threads.

Any help will be appreciated. Thanx, Sunil.


Unless you literally have more than one processor, I'd say forget
it. Unfortunately, the question is off-topic for this group. Try
comp.programmin g, or a group specific to your C++ implementation.

--
Neil Cerutti
Nov 23 '05 #2

su*******@hotma il.com skrev:
Here is a small sample program that I have.
[snip]
Now when I run this with 1 thread, here is the time taken.

/home/skher/testIPC/testThr> time $BIN/testThr 1 5000000

real 0m0.65s
user 0m0.47s
sys 0m0.14s
/home/skher/testIPC/testThr>

impressive, considering I am doing 5 million iterations. So, I thought
when I run with 2 or more threads, I should be done even in less time.
But here is what I found.

/home/skher/testIPC/testThr> time $BIN/testThr 2 5000000

real 0m34.67s
user 0m58.48s
sys 0m5.20s
/home/skher/testIPC/testThr>

Why is this? I guess this is because whenever I allocate any STL
object, using the _node_alloc template defined in _alloc.c, it has a
lock and unlock mechanism using a static class _Node_Alloc_Loc k which
has a static member variable.

[snip]
I have not examined your code in depth, but it looks like there is a
difference in what is done in a one-thread scenario and in a n-thread
scenario, Still - it does not matter as this probably is not the
correct group. Off-hand I know that memory allocation can be much more
expensive in a multithreaded environment, so this is certainly a
factor. Also it is not certain that using more threads will make your
program faster. If the task is CPU-bound this requires hardware with
multiple CPUs. But better ask in comp.programmin g.threads or a group
dedicated to your platform.

Peter

Nov 24 '05 #3
Yes, I would ask it in that group but since both of you have asked the
same question - I am indeed using a 4 CPU machine running Sun Solaris.
Thanx, Sunil.

Nov 24 '05 #4
su*******@hotma il.com wrote:
Yes, I would ask it in that group but since both of you have asked the
same question - I am indeed using a 4 CPU machine running Sun Solaris.
Thanx, Sunil.


It's been a long time since I used Solaris, but I seem to remember
that the pthreads implementation on Solaris did not use multiple
CPU's, yet the Solaris native thread library (lwp ?) did use
multiple CPU's. You'd better ask on a Solaris newsgroup to
get definitive answers.

Pthreads on Linux (2.6+), I believe, will use multiple CPU's.

Regards,
Larry
Nov 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2294
by: andrewpalumbo | last post by:
I'm trying to write some code which will split up a vector into two halves and run a method on the objects in the vector using two seperate threads. I was hoping to see a near linear speedup on an SMP machine, but I'm finding that the code below takes almost exactly the same amount of time as when I iterate through the vector, and don't use any threads at all (using only one processor). I'm running this on a Dual Athlon machine under...
6
3191
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
34
10772
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
2
1702
by: R. Nachtsturm | last post by:
Hi, i have the problem that when i create a low priority background thread, start it, and wait for it to finish that it does not seem to terminate even after it is finished.. if i use performance monitoring to watch the actual thread count then it never goes down.. everytime i create a thread it goes up by one.. even if i abort the thread and set the thread object = nothing it still doesn't decrease
35
4009
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire contents of memory into the new process. There's also a more expensive context switch between...
35
9327
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from 500000 to 3200000 of a file whose size is say 20MB... how do i request a download which starts directly at 500000th byte... thank u cheers
16
15546
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be faster, so I'd like to try creating multiple threads, and having them load different chunks of the file at the same time and process it asynchronously. Is it possible to do something like that, and if so, what would be needed to do so?
4
2252
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check the number of processors and start a multiple of that, but then I remembered KISS and it seemed that...
23
4269
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Recently I had a new coworker. There is some dispute between us. The last company he worked for has a special networking programming model. They split the business logic into different modules, and have a dedicated thread for the each module. Modules exchanged info through a in-memory message queue. In my opinion, such a model means very complicated asynchronous
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.