Ich habe ein - wohl eher kleines - problemchen mit Threads. Gerade habe ich ein HelloWorld Programm geschrieben (im originalen Programm funktionierte die Sache mit den Threads schon nicht, da wollte ich ein kleines Testprogramm schreiben um die sache besser nachvollziehen zu können)
Mein Problem ist, dass die Threadfunktion nicht ausgeführt wird, ein dort gesetzter Haltepunkt bringt da auch nichts und unter cmd liefert das Programm auch keine Ausgabe.
EDIT: Problem hat sich erledigt. Der Thread wird natürlich nur ausgeführt, wärend das Programm näch läuft. Dh. muss nach dem Starten des Threads mindestens ein for(;; herbei. Somit sollte sich DIESES Problem erledigt haben.
Hier ist mal der Code:
---------------------------
stapel.h:
#pragma once #include <string> using namespace std; struct stapelElement { struct stapelElement *next; string data; }; class stapel { private: struct stapelElement *begin; int elementCount; public: stapel(void); ~stapel(void); void push(string); string pop(void); int getCount(void); };
stapel.cpp:
#include "StdAfx.h" #include "stapel.h" stapel::stapel(void) { this->begin = NULL; this->elementCount = 0; } stapel::~stapel(void) { struct stapelElement *t; while (this->begin) { t = this->begin; this->begin = t->next; delete t; } } void stapel::push(string s) { struct stapelElement *tmp; tmp = new struct stapelElement; tmp->next = this->begin; this->begin = tmp; tmp->data = s; this->elementCount++; } string stapel::pop(void) { string r; struct stapelElement *tmp; if (this->elementCount <= 0) { string no; no = "(NULL)"; return no; } tmp = this->begin; this->begin = tmp; r = tmp->data; delete tmp; this->elementCount--; return r; } int stapel::getCount(void) { return this->elementCount; }
cons1.cpp
// cons1.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung. // #include "stdafx.h" #include <windows.h> #include <process.h> #include "stapel.h" stapel *s; void f(void *arg) { int c; string ret; puts("HELLO THREAD!!"); c = s->getCount(); while © { ret = s->pop(); printf("%i: %s\n", c, ret.c_str()); c--; } ret = s->pop(); printf("no more elements: %s\n", ret.c_str()); _endthread(); } int _tmain(int argc, _TCHAR* argv[]) { s = new stapel(); s->push("test!!"); s->push("ein"); s->push("ist"); s->push("dies"); _beginthread(f, 0, s); delete s; return 0; }
Dieser Beitrag wurde von presswurst bearbeitet: 02. August 2008 - 06:13