I create some threads in a for loop and after this loop, join them in other loop. they do their function till all of them finish it,do they? my last result is logically wrong . my result is correct, just when join each thread after create it!!
Yes i think you are doing right.Letsee for example
extern "C"
{
#include <pthread.h>
#include <unistd.h>
}
#include <iostream>
using namespace std;
const int NUMBER_OF_THREADS = 5;
void * thread_talk(void * thread_nr)
{
//do some operation here
pthread_exit(NULL); //exit from current thread
}
int main()
{
pthread_t thread[NUMBER_OF_THREADS];
cout << "Starting all threads..." << endl;
int temp_arg[NUMBER_OF_THREADS] ;
/*creating all threads*/
for(int current_t = 0; current_t < NUMBER_OF_THREADS; current_t++)
{
temp_arg[current_t] = current_t;
int result = pthread_create(&thread[current_t], NULL, thread_talk, static_cast<void*>(&temp_arg[current_t])) ;
if (result !=0)
{
cout << "Error creating thread " << current_t << ". Return code:" << result << endl;
}
}
/*creating all threads*/
/*Joining all threads*/
for(int current_t = 0; current_t < NUMBER_OF_THREADS; current_t++)
{
pthread_join(thread[current_t], NULL);
}
/*Joining all threads*/
cout << "All threads completed." ;
return 0;
}
Its your decision when you want to exit that thread by calling pthread_exit function .Absolutely there is no certainty that which thread will be executed first.Your OS will decide when resources are available for your threads and execute them on whatever CPU is least occupied
Related
I'm trying to write a program to simulate an aircraft carrier running on threads in Cand I don't know why my threads seem to run after each other
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define N 15 // sapaces for planes on carrier
#define K 9 // a number less than n, and if there are currently fewer than K aircraft on the carrier, landing aircraft have priority
#define S 1
pthread_mutex_t mutex;
pthread_cond_t landing_cond;
pthread_cond_t takeoff_cond;
int planes_on_deck = 0;
void* landing() {
while(1) {
pthread_mutex_lock(&mutex);
while (planes_on_deck>=N) {
pthread_cond_wait(&landing_cond, &mutex);
}
planes_on_deck++;
printf("landing.\n");
Sleep(1000);
printf("number of planes on carrier: %d\n\n", planes_on_deck);
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&takeoff_cond);
}
return 0;
}
void* takeoff() {
while(1) {
pthread_mutex_lock(&mutex);
while (planes_on_deck < K) {
pthread_cond_wait(&takeoff_cond, &mutex);
}
planes_on_deck--;
printf("takeoff.\n");
Sleep(1000);
printf("number of planes on carrier: %d\n\n", planes_on_deck);
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&landing_cond);
}
return 0;
}
int main() {
printf("number of planes on carrier: %d\n", planes_on_deck);
pthread_t landing_threads[S];
pthread_t takeoff_threads[S];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&landing_cond, NULL);
pthread_cond_init(&takeoff_cond, NULL);
for (int i = 0; i < S; i++){
if (pthread_create(&landing_threads[i], NULL, &landing, NULL) != 0){
perror("fail");
}
if (pthread_create(&takeoff_threads[i], NULL, &takeoff, NULL) != 0){
perror("fail");
}
}
for (int i = 0; i < S; i++){
if (pthread_join(takeoff_threads[i], NULL) != 0){
return 2;
}
if (pthread_join(landing_threads[i], NULL) != 0){
return 2;
}
}
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&landing_cond);
pthread_cond_destroy(&takeoff_cond);
return 0;
}
at the beginning it starts to list landing planes as it should and when it reaches K it should give the opportunity for planes to take off
but he does so only when he reaches the space limit on the ship
Maybe you can tell me, what's wrong with my program, why it behaves the way it does
why it behaves the way it does
You call Sleep() with the mutex held. That prevents the other thread from doing anything. You should unlock the mutex before going to sleep.
In general, sleeping (or doing other long operations, like reading from a remote socket or opening a file) with a mutex held is almost always wrong.
How to get stack trace for C/C++ program in CYGWIN environment ?
** I was looking for a back trace mechanism, I've compiled some of the solutions found here and made it a small program for quick reference.
My Answers with a code snippet:
#if defined(__CYGWIN__)
#include <Windows.h>
#include <dbghelp.h>
#include <psdk_inc/_dbg_common.h>
#include <cxxabi.h>
#include <cstring>
class Error // Windows version
{
private:
void *stacktrace[MAX_STACKTRACE_SIZE];
size_t stacktrace_size;
public:
const char* message;
Error(const char* m)
: message(m)
, stacktrace_size(0)
{
// Capture the stack, when error is 'hit'
stacktrace_size = CaptureStackBackTrace(0, MAX_STACKTRACE_SIZE, stacktrace, nullptr);
}
void print_backtrace(ostream& out) const
{
SYMBOL_INFO * symbol;
HANDLE process;
size_t length;
process = GetCurrentProcess();
SymInitialize(process, nullptr, TRUE);
symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
length = strlen (symbol->Name);
std::string result;
char tempStr[255] = {0};
for (int i = 0; i < stacktrace_size; i++)
{
int status = 0;
// '_' is missing in symbol->Name , hence prefix it and concat with symbol->Name
char prefixed_symbol [256] = "_" ;
SymFromAddr(process, (DWORD64)(stacktrace[i]), 0, symbol);
auto backtrace_line = string(symbol->Name);
if (backtrace_line.size() == 0) continue;
// https://en.wikipedia.org/wiki/Name_mangling
// Prefix '_' with symbol name, so that __cxa_demangle does the job correctly
// $ c++filt -n _Z9test_ringI12SmallIntegerIhEEvRK4RingIT_E
strcat (prefixed_symbol, symbol->Name);
char * demangled_name = abi::__cxa_demangle(prefixed_symbol, nullptr, nullptr, &status);
if(status < 0)
{
sprintf(tempStr, "%i: %s - 0x%0X\n", stacktrace_size-i-1, symbol->Name, symbol->Address);
// out << symbol->Name << endl;
}
else
{
sprintf(tempStr, "%i: %s - 0x%0X\n", stacktrace_size - i - 1, demangled_name, symbol->Address);
// out << demangled_name << endl;
}
// Append the extracted info to the result
result += tempStr;
// Free the HEAP allocation made by __cxa_demangle
free((void*)demangled_name);
// Restore the prefix '_' string
prefixed_symbol [1] = '\0';
}
std::cout << result << std::endl;
free(symbol);
}
};
int main ()
{
try {
do_something ();
if (false == status) throw Error("SystemError");
}
catch (const Error &error)
{
cout << "NotImplementedError(\"" << error.message << "\")" << endl;
error.print_backtrace(cout);
return 1;
}
#endif
Command Line Option:
// Use -limagehlp to link the library
g++ -std=c++20 main.cpp -limagehlp
In libc++ i have found that basic_string destructor does not gets called , once string goes out of the scope the memory is freed by calling delete operator rather than calling its destructor and then calling the delete operator from destructor, why so?
Can some one explain this?
see the sample program
void * operator new ( size_t len ) throw ( std::bad_alloc )
{
void * mem = malloc( len );
if ( (mem == 0) && (len != 0) )
throw std::bad_alloc();
return mem;
}
void operator delete ( void * ptr ) throw()
{
if ( ptr != 0 )
free( ptr );
}
int main(int argc, const char * argv[])
{
std::string mystr("testing very very very big string for string class");
std::string mystr2(mystr1.begin(),mystr.end());
}
Put break point on new and delete and then check the call stack.
new operator gets called from basic_string class while the delete gets called from the end of main, while ideally basic_string destructor should have called first and then the delete operator should be called via deallocate call of allocator, this is valid for 2nd string creation.
I'm seeing the same thing in the debugger that you are; I don't know for sure, but I suspect that stuff is getting inlined. The destructor for basic_string is very small; a single test (for the small string optimization), and then a call to the allocator's deallocate function (through allocate_traits). std::allocators allocate function is also quite small, just a wrapper around operator delete.
You could test this by writing your own allocator. (Later: see below)
More stuff that I generated while investigating this question; read on if you're interested.
[Note: there's a bug in your code - in the second line you wrote: mystr1.begin(),mystr.end()) - where is mystr1 declared?]
Assuming that's a typo, I tried some slightly different code:
#include <string>
#include <new>
#include <iostream>
int news = 0;
int dels = 0;
void * operator new ( size_t len ) throw ( std::bad_alloc )
{
void * mem = malloc( len );
if ( (mem == 0) && (len != 0) )
throw std::bad_alloc();
++news;
return mem;
}
void operator delete ( void * ptr ) throw()
{
++dels;
if ( ptr != 0 )
free( ptr );
}
int main(int argc, const char * argv[])
{
{
std::string mystr("testing very very very big string for string class");
std::string mystr2(mystr.begin(),mystr.end());
std::cout << "News = " << news << "; Dels = " << dels << std::endl;
}
std::cout << "News = " << news << "; Dels = " << dels << std::endl;
}
If you run this code, it prints (at least for me):
News = 2; Dels = 0
News = 2; Dels = 2
which is exactly what it should.
If I toss the code into compiler explorer, then I see both the calls to basic_string::~basic_string(), exactly as I expect. (Well, I see three of them, but one of them is in an exception handling block, which ends with a call to _Unwind_resume).
Later - this code:
#include <string>
#include <new>
#include <iostream>
int news = 0;
int dels = 0;
template <class T>
class MyAllocator
{
public:
typedef T value_type;
MyAllocator() noexcept {}
template <class U>
MyAllocator(MyAllocator<U>) noexcept {}
T* allocate(std::size_t n)
{
++news;
return static_cast<T*>(::operator new(n*sizeof(T)));
}
void deallocate(T* p, std::size_t)
{
++dels;
return ::operator delete(static_cast<void*>(p));
}
friend bool operator==(MyAllocator, MyAllocator) {return true;}
friend bool operator!=(MyAllocator, MyAllocator) {return false;}
};
int main(int argc, const char * argv[])
{
{
typedef std::basic_string<char, std::char_traits<char>, MyAllocator<char>> S;
S mystr("testing very very very big string for string class");
S mystr2(mystr.begin(),mystr.end());
std::cout << "Allocator News = " << news << "; Allocator Dels = " << dels << std::endl;
}
std::cout << "Allocator News = " << news << "; Allocator Dels = " << dels << std::endl;
}
prints:
Allocator News = 2; Allocator Dels = 0
Allocator News = 2; Allocator Dels = 2
which confirms that the allocator is getting called.
I am trying to read in from the following file:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
12345abcdefghijklmnopqrstu
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
The code is below:
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <mutex>
#include <vector>
#include <array>
#include <algorithm>
#include <iterator>
#define CHUNK_SIZE 55
std::mutex queueDumpMutex;
void getLinesFromChunk(std::vector<char>& chunk, std::vector<std::string>& container)
{
static std::string str;
unsigned int i = 0;
while(i < chunk.size())
{
str.clear();
size_t chunk_sz = chunk.size();
while(chunk[i] != '\n' && i < chunk_sz )
{
str.push_back(chunk[i++]);
}
std::cout<<"\nStr = "<<str;
if (i < chunk_sz)
{
std::lock_guard<std::mutex> lock(queueDumpMutex);
container.push_back(str);
}
++i;
}
chunk.clear();
std::copy(str.begin(), str.end(), std::back_inserter(chunk));
std::cout << "\nPrinting the chunk out ....." << std::endl;
std::copy(chunk.begin(), chunk.end(), std::ostream_iterator<char>(std::cout, " "));
}
void ReadFileAndPopulateDump(std::ifstream& in)
{
std::vector<char> chunk;
chunk.reserve(CHUNK_SIZE*2);
std::vector<std::string> queueDump;
in.unsetf(std::ios::skipws);
std::cout << "Chunk capacity: " << chunk.capacity() << std::endl;
do{
in.read(&chunk[chunk.size()], CHUNK_SIZE);
std::cout << "Chunk size before getLines: " << chunk.size() << std::endl;
getLinesFromChunk(chunk, queueDump);
std::cout << "Chunk size after getLines: " << chunk.size() << std::endl;
}while(!in.eof());
}
int main()
{
std::ifstream in("/home/ankit/codes/more_practice/sample.txt", std::ifstream::binary);
ReadFileAndPopulateDump(in);
return 0;
}
What i wish to achieve is for the container to be line complete.
By this i mean that suppose my CHUNK_SIZE reads only:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
12
The container should look like:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
instead of:
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz12
Now i understand that chunk.reserve(CHUNK_SIZE) reserves the given memory and does not actually assign a SIZE. Because if this i am not able to read from in.read().
If i use chunk.resize(CHUNK_SIZE) and append it to the end as i want the remaining characters '12' to be appended with its complete line.
Now the issue is that the code is being repeated more than it should. According to me the conditions seem fine.
Any help will be much appreciated.
Sorry but I don't understand why do you:
read the file in binary mode and not in text mode
don't use getline()
use a vector<char> instead a string
For what I understand the problem you propose, I would do it this way
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::ifstream f("sample.txt"); // text mode!
std::size_t const chunkSizeMax = 55U;
std::string str;
std::string chunk;
while ( std::getline(f, str) )
{
if ( chunkSizeMax <= (chunk.size() + str.size()) )
{
std::cout << "chunk: [" << chunk << "]\n";
chunk.clear();
}
chunk += str;
}
std::cout << "last chunk: [" << chunk << "]\n";
return EXIT_SUCCESS;
}
Hoping this helps.
Alright, i have a hw assignment to check if an inserted string is a palindrome. The string must first be inserted into a stack, a queue, and then compared. I have the program up and running, for me that is. My teacher, when trying to grade it experience(d) a run time error. Also, getline portable is a requirement of the assignment and came with the file and instructions.
This is the note from the teacher:
Check if a line is a palindrome. Ignore spaces? y/n y (her running the code)
Input line to check
(where she gets the runtime error)
175 [main] csc240Summer2014PE8Student 10060 open_stackdumpfile: Dumping stack trace to csc240Summer2014PE8Student.exe.stackdump
#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;
istream& getline_portable( istream& is, string& str ) {
istream& ris = std::getline(is,str);
if ( str.size() && str[str.size()-1] == '\r' )
str.resize(str.size()-1);
return ris;
}
int main()
{
stack<char> s;
queue<char> q;
char c, choice, b;
string str;
int i = 0;
int count = 0;
do
{
cout<<"Check if a line is a palindrome. Ignore spaces? y/n ";
cin >> b;
cin.ignore();
tolower(b);
cout<<"Input line to check\n";
getline_portable(cin,str);
for(int j = 0; j < str.size(); j++)
str[j] = tolower(str[j]);
if(b == 'n')
{
for(int j = 0; j < str.size(); j++)
{
c = str[j];
q.push(c);
s.push(c);
}
}
else if (b == 'y')
{
for(int j = 0; j < str.size() - count; j++)
{
c = str[j];
if(isspace(c))
{
}
else if(!isspace(c))
{
q.push(c);
s.push(c);
}
}
}
do
{
if(q.front() != s.top())
{
i = false;
break;
}
else
{
i = true;
s.pop();
q.pop();
}
}while(!q.empty() && !s.empty());
if (i == true)
cout << str << " is a pallindrom.\n";
else if (i == false)
cout << "Your input of " << str << " is not a pallindrome.\n";
cout << "Would you like to test another string? y/n ";
cin >> choice;
tolower(choice);
cin.ignore();
}while (choice == 'y');
cout << "Press enter to continue...";
cin.get();
return 0;
}