473,320 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

creating pthreads runtime

Hi,
I 'm facing some prob with creating pthreads.
My requirement is to create threads in a loop.
but I dont want to make an array of pthread.

At anytime I want to have 5 threads (I used a global variable as a
counter).
Whenever the counter is less than 5, I want to create new thread.

whenever any thread is about to terminate, I will decrement the
counter. (of course avoiding race conditions using mutex lock).

But 'm confused how to create thread at run time.

I tried something like
pthread_mutex_lock(&check_thread_count);

if(threadCounter<5){
pthread_create(new pthread(), NULL, handleConnection, NULL);
}

pthread_mutex_unlock(&check_thread_count);

using *new pthread()* is not correct.

So how can I create pthread at run time?
Sep 23 '08 #1
4 4037
sorry friends,
I made syntax error there...
I got the solution..
It should be

new pthread_t()

i missed "_t"

Happy Programming :)
Sep 23 '08 #2
Neel wrote:
>
I'm facing some prob with creating pthreads.
My requirement is to create threads in a loop.
but I dont want to make an array of pthread.
.... snip ...
>
using *new pthread()* is not correct.

So how can I create pthread at run time?
There are no pthreads in the C language. Thus this is off-topic.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 23 '08 #3
Neel <a.******@gmail.comwrites:
I 'm facing some prob with creating pthreads.
[...]

Try asking in comp.programming.threads.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 23 '08 #4
On 23 Sep 2008 at 2:32, Neel wrote:
On On 23 Sep 2008 at 2:02, Neel wrote:
if(threadCounter<5){
pthread_create(new pthread(), NULL, handleConnection, NULL);
}

I got the solution..
It should be

new pthread_t()

i missed "_t"
This is a recipe for a memory leak. As you never keep track of the
pthread_t * you get from new (an operator which incidentally is only
available in C++, not in vanilla C), you'll have no way of freeing the
memory with delete once the thread exits.

You should either have a pthread_t variable and pass its address to
pthread_create(), or else keep a record of the pointer from new,
use pthread_join() somewhere appropriate and make sure to delete/free()
the memory used by the pthread_t structure.

Sep 28 '08 #5

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

Similar topics

0
by: Kevin Dahlhausen | last post by:
I've been playing with pySonic on unix. PySonic is pyrex wrapper around the fmod sound library. Fmod is distributed as a shared library that uses pthreads. I am getting a segfault when a...
5
by: Arnd Schroeter | last post by:
Hello! I am a c++ programmer under the os linux. I am using pthreads and i am wondering why i can only use satic methods of a class to create a thread of it. How can i change this, because it...
15
by: John David Ratliff | last post by:
Maybe someone here can help me. I want to use C++, but pthreads seems to be nonfunctional in it. The following test code I've written in C and C++. The C version works, while the C++ one does...
2
by: Jack | last post by:
hi all I would find a parallel sorting algorithms written with pthread programs, as a benchmark to run. Any pointing will be of help thankyou in advance
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory...
5
by: Ed L. | last post by:
Is this pthreads warning of any concern? gcc -O2 -fno-strict-aliasing -Wall -Wmissing-prototypes -Wmissing-declarations -pthread -pthreads -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
8
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.